package com.putige.reggie.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.putige.reggie.common.MyThreadLocal;
import com.putige.reggie.entity.AddressBook;
import com.putige.reggie.mapper.AddressBookMapper;
import com.putige.reggie.service.AddressBookService;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class AddressBookServiceImpl extends ServiceImpl<AddressBookMapper, AddressBook> implements AddressBookService {

    @Override
    public boolean deleted(Long[] ids) {
        //类型转换
        List<Long> longs = Arrays.asList(ids);
        //得到所有id循环修改is_delete属性
        List<AddressBook> addressBooks = this.listByIds(longs);
        addressBooks = addressBooks.stream().map(c -> {
            c.setUserId(MyThreadLocal.getCreateUser());
            c.setIsDeleted(1);
            return c;
        }).collect(Collectors.toList());
        this.updateBatchById(addressBooks);
        return true;
    }

    @Override
    public boolean updateAddr(AddressBook addressBook) {
        //通过传入的地址id查询一个对象
        AddressBook byId1 = this.getById(addressBook.getId());
        if (byId1==null){
            return false;
        }
       //当对象存在,传入数据,调用修改方法
        //存在,把登录的userid设置进去再修改
       addressBook.setUserId(MyThreadLocal.getCreateUser());
       this.updateById(addressBook);
        return true;
    }
}